#ifndef TUYA_IOT_KIT_H
#define TUYA_IOT_KIT_H

#include <QObject>
#include <QWidget>
#include <QDebug>
#include <QTimer>
#include <QThread>
#include <QSemaphore>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "tuya_log.h"
#include "tuya_iot.h"
#include "cJSON.h"


#define TUYA_PRODUCT_KEY       "nqvxq8qu6heyla1e"
#define TUYA_DEVICE_UUID       "uuid68ad0d8c1c3c944a"
#define TUYA_DEVICE_AUTHKEY    "WUCOaYVVQxLOzH1xAX1xpufBF2IMIMMB"
/*
https://smartapp.tuya.com/s/p?p=nqvxq8qu6heyla1e&uuid=uuid68ad0d8c1c3c944a&v=2.0
*/


#define SOFTWARE_VERS           "1.0.0"

#define DP_PH_VALUE             "101"
#define DP_BRIGHT_STATE         "102"
#define DP_HUMIDITY             "103"
#define DP_TEMPERATURE          "104"
#define DP_BRIGHT_VALUE         "105"
#define DP_TEMP_UNIT_CONVERT    "106"
#define DP_BATTERY_STATE        "107"
#define DP_BATTERY_PERCENTAGE   "108"
#define DP_MOTOR_CTRL           "109"
#define DP_LIGHT_CTRL           "110"

#define CLOUD_DISCONNECT   0x00
#define CLOUD_CONNECT      0x01

#define FLAG_0   (1 << 0)
#define FLAG_1   (1 << 1)
#define FLAG_2   (1 << 2)
#define FLAG_3   (1 << 3)
#define FLAG_4   (1 << 4)
#define FLAG_5   (1 << 5)
#define FLAG_6   (1 << 6)
#define FLAG_7   (1 << 7)

typedef struct iot_data
{
    unsigned int handle_flag;
    unsigned char cloud_state;
    bool motor_ctrl;
    bool light_ctrl;
}iot_data_t;



extern iot_data_t iot_data;
extern tuya_event_msg_t event_msg;
extern tuya_iot_client_t *m_client;
extern tuya_iot_client_t client;
class Tuya_IoT_Kit_Qthread : public QThread
{
    Q_OBJECT

public:
    explicit Tuya_IoT_Kit_Qthread(QObject *parent = nullptr);
    ~Tuya_IoT_Kit_Qthread();


    static void tuya_iot_dp_download(tuya_iot_client_t* client, const char* json_dps);

    static void tuya_iot_report_bool_value(char *dp, bool flag);
    static void tuya_iot_report_int_value(char *dp, int value);
    static void tuya_iot_report_string_value(char *dp, char *str);
    static void tuya_iot_report_enum_value(char *dp, int enum_value);
    static void tuya_iot_report_raw_value(char *dp, char *p_buff);



    static int dp_motor_ctrl_process(cJSON *p_obj);
    static int dp_light_ctrl_process(cJSON *p_obj);


protected:
    void run() override;

private:

    int tuya_iot_kit_init(void);
    static void user_event_handler_on(tuya_iot_client_t* client, tuya_event_msg_t* event);

};
#endif // TUYA_IOT_KIT_H
